const app = getApp()
//获得全局js变量
var util = require('../../../utils/util.js');
Page({
    data: {
        formData: {
            referralCode: '', // 添加推荐码字段
            name: '',
            phone: '',
            idCard: '',
            address: '',
            reason: '',
			province: '', 
			provinceCode: '', 
			city: '', 
			cityCode: '', 
			area: "", 
			areaCode:'',
			region: ["请选择地址",'',''],
			job: ''
        },
        professionOptions: [
            '销售', '市场', '客服', '技术', '管理', '其他'
        ],
        selectedProfession: -1,
        agreeProtocol: false,
        submitDisabled: true,
        branchCompanies: [
            { id: 1, name: '北京分公司' },
            { id: 2, name: '上海分公司' },
            { id: 3, name: '广州分公司' },
            { id: 4, name: '深圳分公司' },
            { id: 5, name: '成都分公司' },
            { id: 6, name: '杭州分公司' }
        ],
        branchCompanyIndex: 0,
        branchCompanyId:0
    },
    
    // 如果页面加载时有推荐码参数，自动填充
    onLoad: function(options) {
        if (options.referralCode) {
            this.setData({
                'formData.referralCode': options.referralCode
            });
        }
        // 获取用户基本信息
        const app = getApp();
        if (app.globalData.userInfo) {
            this.setData({
                'formData.name': app.globalData.userInfo.name || '',
                'formData.phone': app.globalData.userInfo.phone || ''
            });
        }
		this.getConApply();
        this.getCompany();
    },
    getCompany: function() {
        var that = this;
        util.httpRrquest(app.data.rooturl + 'user/getCompany', 'GET')
        .then(function (response) {
            if (response.data.code == 1) {
                var data = response.data.data;
                that.setData({
                    branchCompanies: data
                });
            }
        })
    },
    getConApply:function(){
		var that=this;
        util.httpRrquest(app.data.rooturl + 'user/getConApply', 'GET')
        .then(function (response) {
			var data = response.data.data;
			if(data.hasData == 1){
				var data = data.data;
				that.setData({
					'formData.referralCode': data.pCode,
					'formData.region': data.region,
					'formData.province': data.province,
					'formData.provinceCode': data.provinceCode,
					'formData.city': data.city,
					'formData.cityCode': data.cityCode,
					'formData.area': data.area,
					'formData.areaCode': data.areaCode,
					'formData.reason': data.reason,
					'formData.address': data.address,
					'formData.idCard': data.idCardNum,
					'formData.name': data.name,
					'formData.phone': data.phone,
					'formData.remark': data.remark,
					'formData.job': data.job,
					'formData.region': [data.province,data.city,data.area],
                    status:data.status,
                    professionOptions:response.data.data.job,
                    selectedProfession:response.data.data.selectedProfession,
                    branchCompanyIndex:data.companyIndex
				})
			}
        }, function (error) {
            console.log(error);
        });
	},
    bindRegionChange: function (e) {
        var value = e.detail.value; 
        var code = e.detail.code; 
        this.setData({
            'formData.region': e.detail.value,
            'formData.province': value[0],
            'formData.provinceCode': code[0],
            'formData.city': value[1],
            'formData.cityCode': code[1],
            'formData.area': value[2],
            'formData.areaCode': code[2]
        })
    },
    // 输入框内容变化
    inputChange: function(e) {
        const { field } = e.currentTarget.dataset;
        const { value } = e.detail;
        
        this.setData({
            [`formData.${field}`]: value
        });
        
        this.checkFormValid();
    },
    
    // 选择职业
    selectProfession: function(e) {
        const index = e.currentTarget.dataset.index;
        
        this.setData({
            selectedProfession: index,
            'formData.profession': this.data.professionOptions[index],
            job: this.data.professionOptions[index]
        });
        
        this.checkFormValid();
    },
    
    // 协议勾选状态变化
    protocolChange: function(e) {
        this.setData({
            agreeProtocol: e.detail.value.length > 0
        });
        
        this.checkFormValid();
    },
    
    // 检查表单是否有效
    checkFormValid: function() {
        const { name, phone, idCard, address, profession, reason } = this.data.formData;
        const isValid = name && phone && idCard && address && profession && reason && this.data.agreeProtocol;
        
        this.setData({
            submitDisabled: !isValid
        });
    },
    
    // 查看协议
    viewProtocol: function() {
        wx.navigateTo({
            url: '/pages/partner/protocol/protocol',
        });
    },
    
    // 分公司选择变化
    branchCompanyChange: function(e) {
        const index = e.detail.value;
        this.setData({
            branchCompanyIndex: index,
            'formData.branchCompanyId': this.data.branchCompanies[index].id,
            'formData.branchCompanyName': this.data.branchCompanies[index].name
        });
        this.checkFormValid(); // 如果有表单验证函数，需要调用
    },
    
    // 提交申请
    submitApply: function() {
        if (this.data.submitDisabled) {
            return;
        }
        util.httpRrquest(app.data.rooturl + 'user/editConApply', 'POST',this.data.formData)
        .then(function (response) {
            if (response.data.code == 1) {
                wx.showModal({
                    title: '申请提交成功',
                    content: '您的合伙人申请已提交，我们将在1-3个工作日内审核，请留意通知。',
                    showCancel: false,
                    success: () => {
                        wx.navigateBack();
                    }
                });	
            }
        }, function (error) {
            console.log(error);
        });
        
        // 确保表单数据中包含分公司信息
        const formData = this.data.formData;
        if (!formData.branchCompanyId) {
            wx.showToast({
                title: '请选择分公司',
                icon: 'none'
            });
            return;
        }
        
    }
})